//
//  RootViewController.m
//  UI06_无限滚动的相册
//
//  Created by dllo on 15/9/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface RootViewController ()<UIScrollViewDelegate>

@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UIPageControl *page;

@end

@implementation RootViewController
- (void)dealloc{
    [_scrollView release];
    [super dealloc];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    // 移动的范围
    self.scrollView.contentSize = CGSizeMake(WIDTH * 9, 0);
    // 按页滚动
    self.scrollView.pagingEnabled = YES;
    
    // 创建imageViewFirst
    UIImageView *imageViewFirst = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    imageViewFirst.image = [UIImage imageNamed:@"h7.jpeg"];
    [self.scrollView addSubview:imageViewFirst];
    [imageViewFirst release];
    imageViewFirst.userInteractionEnabled = YES;  // 打开图片的用户交互
    
    // 通过for循环,把一到七的视图创建出来
    for (NSInteger i = 1; i < 8; i++) {
        NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg", i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:imageName];
        [self.scrollView addSubview:imageView];
        [imageView release];
        imageView.userInteractionEnabled = YES;
    }
    
    // 创建imageViewLast
    UIImageView *imageViewLast = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH , HEIGHT)];
    imageViewLast.image = [UIImage imageNamed:@"h1.jpeg"];
    [self.scrollView addSubview:imageViewLast];
    [imageViewLast release];
    imageViewLast.userInteractionEnabled = YES;
    
    // 为了能显示第一张图片,我们需要先设置一个偏移量, 这样能直接显示第一张图片
    self.scrollView.contentOffset = CGPointMake(WIDTH , 0);
    
    // 设置代理人
    self.scrollView.delegate = self;
    
    
    // 创建一个pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 635, 200, 40)];
    [self.view addSubview:self.page];
//    self.page.backgroundColor = [UIColor grayColor];
    self.page.numberOfPages = 7;  // 点的个数
    self.page.currentPageIndicatorTintColor = [UIColor blackColor]; // 当前选中点得颜色
    self.page.pageIndicatorTintColor = [UIColor grayColor];  // 其他点的颜色
    [_page release];
    
    // 对图片进行缩放, 这个方法有bug,一般不用
//    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
//    
//    //想要scrollView实现缩放功能, 需要先设置最大最小的缩放比例
//    self.scrollView.maximumZoomScale = 2;
//    self.scrollView.minimumZoomScale = 0.5;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"1111" forState:(UIControlStateNormal)];
    button.backgroundColor = [UIColor blackColor];
    button.frame = CGRectMake(WIDTH * 5 + WIDTH / 2, 300, 50, 50);
    [self.scrollView addSubview:button];
    [button addTarget:self action:@selector(clickbutton:) forControlEvents:(UIControlEventTouchUpInside)];
}
- (void)clickbutton:(UIButton *)button{
    NSLog(@"111111");
}

#pragma mark 这个方法用来管理scrollView的缩放功能
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return [scrollView.subviews firstObject];
}

// pageAction
- (void)pageAction:(UIPageControl *)page{
    
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * WIDTH, 0) animated:YES];
}

// 滑动结束, 触发的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x == WIDTH * 8 ) {
        scrollView.contentOffset = CGPointMake(WIDTH, 0);
    } else if (scrollView.contentOffset.x == 0){
        scrollView.contentOffset = CGPointMake(WIDTH * 7, 0);
    }
    
    // pageControl和scrollView 进行关联, 点的移动方向和scrollView的偏移量有关
    // 无论怎么方向移动, 当pageControl和scrollView关联的时候, 偏移量都是修改好的, 所以可以正确的滚动和显示
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
